package org.myorg;

/*
 * Apended on the OOP, gene should be a class, but this will
 * add the difficult of implement, if you like, you can add a gene 
 * class :) 
 */

public class Ps extends Gene{
	
	int length;
	String gene = "";
	float fitness = 0;

	public Ps(int length) {
		this.length = length;
		for(int i = 0; i < length; i++) {
			if (Math.random() > 0.5) {
				gene += "0";
			} else {
				gene += "1";
			}
		}
	}
	
	public Ps(String gene) {
		this.gene = gene;
		this.length = gene.length();
	}

	public String getGene() {
		return gene;
	}
	public float getFitness() {
		return fitness;
	}

	public void setFitness(float fitness) {
		this.fitness = fitness;
	}
	
	public String getGene(int begin) {
		return gene.substring(begin);
	}
	
	public String getGene(int begin,int end) {
		return gene.substring(begin, end);
	}
	
	public char geneAt(int pos) {
		return gene.charAt(pos);
	}
	
	public Ps setGene(int pos, char value) {
		char temp[]; 
		temp = gene.toCharArray();
		temp[pos] = value;
		return new Ps(new String(temp));
	}
	
	static public Ps linkGene(Ps subGene1, Ps subGene2) {
		return new Ps(subGene1.getGene() + subGene2.getGene());
	}
	
	static public Ps linkGene(String subGene1, String subGene2) {
		return new Ps(subGene1 + subGene2);
	}
	
}
